package edu.unice.simul.vue.initialisation.modes_dessin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

import edu.unice.simul.controleur.initialisation.InitialisationControleur;

/**
 * Toolbar utilisée lors du dessin de la pièce
 * 
 * @author Romain Ciaccafava
 * 
 */
public class DrawToolBar extends JToolBar {
	private static final long serialVersionUID = -9065334389018223412L;

	private SupportModesDessin support;
	private InitialisationControleur controleur;

	/* ImageIcons */
	private ImageIcon penIcon = new ImageIcon("./res/pen.png");
	private ImageIcon lineIcon = new ImageIcon("./res/line.png");
	private ImageIcon murIcon = new ImageIcon("./res/wall.png");
	private ImageIcon sortieIcon = new ImageIcon("./res/door.png");
	private ImageIcon humainIcon = new ImageIcon("./res/human.png");
	private ImageIcon effacerIcon = new ImageIcon("./res/trash.png");
	private ImageIcon toutEffacerIcon = new ImageIcon("./res/undo.png");

	/* Buttons */
	private JToggleButton pen = new JToggleButton(penIcon);
	private JToggleButton line = new JToggleButton(lineIcon);
	private JButton mur = new JButton(murIcon);
	private JButton sortie = new JButton(sortieIcon);
	private JButton humain = new JButton(humainIcon);
	private JButton effacer = new JButton(effacerIcon);
	private JButton toutEffacer = new JButton(toutEffacerIcon);

	public DrawToolBar(SupportModesDessin support,
			InitialisationControleur controleur) {
		super("Type de bloc", VERTICAL);
		this.controleur = controleur;
		this.support = support;
		buildUI();
	}

	/**
	 * Construit l'interface
	 */
	private void buildUI() {
		setFloatable(false);

		pen.setToolTipText("Dessin au crayon");
		line.setToolTipText("Dessin en lignes");
		mur.setToolTipText("Mur");
		sortie.setToolTipText("Sortie");
		humain.setToolTipText("Humain");
		effacer.setToolTipText("Effacer");
		toutEffacer.setToolTipText("Tout effacer");

		add(pen);
		add(line);
		addSeparator();
		add(mur);
		add(sortie);
		add(humain);
		add(effacer);
		addSeparator();
		add(toutEffacer);

		pen.setSelected(true);
		support.pen();

		addListeners();
	}

	/**
	 * Ajoute les écouteurs à l'interface
	 */
	private void addListeners() {
		mur.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				support.modeMur();
			}
		});

		sortie.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				support.modeSortie();
			}
		});

		humain.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				support.modeHumain();
			}
		});

		effacer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				support.effacer();
			}
		});

		toutEffacer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controleur.clear();
			}
		});

		pen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (pen.isSelected()) {
					line.setSelected(false);
					support.pen();
				}
			}
		});

		line.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (line.isSelected()) {
					pen.setSelected(false);
					support.line();
				}
			}
		});
	}
}
